import { PayloadUser } from '@libs/common/decorator/payload-user.decorator';
import { IndexInterceptor } from '@libs/common/interceptors';
import { Body, Controller, Post, UseInterceptors } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import {
  IndexPointsHistoryDto,
  ShowPointsHistoryDto,
} from './dto/points-history.dto';
import { PointsHistory } from './entities/points-history.entity';
import {
  PointsHistoryService,
  productFormatter,
} from './points-history.service';

@ApiTags('积分记录')
@Controller('market/points-history')
export class PointsHistoryController {
  constructor(private readonly pointsHistoryService: PointsHistoryService) {}

  @Post('index')
  @UseInterceptors(new IndexInterceptor())
  index(@Body() dto: IndexPointsHistoryDto) {
    return this.pointsHistoryService.index(dto);
  }

  @Post('show')
  show(@Body() dto: ShowPointsHistoryDto) {
    return this.pointsHistoryService.show(dto);
  }

  @Post('user')
  @UseInterceptors(
    new IndexInterceptor<PointsHistory>({
      formatter: (v) => productFormatter(v),
    }),
  )
  user(
    @Body() dto: IndexPointsHistoryDto,
    @PayloadUser() user: SC.User.SysInfo,
  ) {
    return this.pointsHistoryService.findUserPointsHistory(dto, user.id);
  }
}
